//
//  YDYSliderViewController.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/9/27.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYSliderViewController.h"

#define ydy_sliderViewAlapha  0.5;

@interface YDYSliderViewController ()<UITableViewDataSource,UITableViewDelegate>
//头像
@property (weak, nonatomic) IBOutlet UIImageView *profileHeadImgV;
//用户名
@property (weak, nonatomic) IBOutlet UILabel *profleNameLab;
@property (weak, nonatomic) IBOutlet UITableView *setTableView;
//监听侧滑
@property (weak, nonatomic) IBOutlet UIView *sliderGerView;

/** 记录是否已经弹出 */
@property (nonatomic, assign) BOOL isshow;
/** 背景阴影 */
@property (nonatomic, weak) UIView *shadowView;
@property (nonatomic, strong) NSArray *settings;
@end

@implementation YDYSliderViewController


#pragma mark -
#pragma mark -外部方法

-(void)show{
    //换个名字，防止冲突;
   
    
//    self.view.frame = CGRectMake(-YDYScreenSize.width, 0, YDYScreenSize.width, YDYScreenSize.height);
    [UIView animateWithDuration:0.5 animations:^{
        
        self.view.transform = CGAffineTransformMakeTranslation(267, 0);
        
        self.shadowView.alpha = ydy_sliderViewAlapha;
//        self.view.frame = CGRectMake(0, 0, YDYScreenSize.width, YDYScreenSize.height);
    }completion:^(BOOL finished) {
//        animatefinished();
    }];
    
    
}

#pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(-267, 0, YDYScreenSize.width, YDYScreenSize.height);
    [self setupTableView];
    [self setupShadowView];
    [self setupSlierGer];
    self.profileHeadImgV.image = [self.profileHeadImgV.image ydy_circleImage];
}
-(void)setupSlierGer{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan2:)];
    [self.sliderGerView addGestureRecognizer:pan];
    
}
-(void)setupTableView{
    self.setTableView.sectionHeaderHeight = 3;
    self.setTableView.backgroundColor = [UIColor whiteColor];
    self.setTableView.rowHeight = 52;
    self.setTableView.bounces = NO;
}
-(void)setupShadowView{
    UIView *shadow = [[UIView alloc]initWithFrame:CGRectMake(0, 0, YDYScreenSize.width * 2, YDYScreenSize.height)];
    shadow.backgroundColor = [UIColor blackColor];
    shadow.alpha = 0 ;
    self.shadowView = shadow;
    
    [self.view addSubview:shadow];
    [self.view sendSubviewToBack:shadow];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan2:)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
    [shadow addGestureRecognizer:tap];
    [shadow addGestureRecognizer:pan];
}
-(void)dismissView{
    
    YDYLog(@"消失");
    
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(-YDYScreenSize.width, 0, YDYScreenSize.width, YDYScreenSize.height);
        self.view.alpha = 0.001;
    }completion:^(BOOL finished) {
//        [self.view removeFromSuperview];
//        self.pushback(self);
    }];

    
}


#pragma mark -
#pragma mark -侧滑
//侧拉打开之后手势调用的方法
-(void)pan2:(UIPanGestureRecognizer *)recognizer{
    //经过多次测试，如果需要改变收回或弹出距离，那只能用2个手势方法去做了。
    
    CGPoint trans = [recognizer translationInView:recognizer.view];
    CGFloat changeX = self.view.transform.tx + trans.x;
    if (changeX < 267 ) {
        self.view.transform = CGAffineTransformTranslate(self.view.transform, trans.x, 0);
        self.shadowView.alpha =  0.8 * changeX *0.001 +0.011;
    }
    
    if (recognizer.state == UIGestureRecognizerStateEnded && changeX < 267 * 0.8) {
        [UIView animateWithDuration:0.2 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(-120, 0);
            self.shadowView.alpha = 0.011;
        }];
        
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:0.2 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(267, 0);
            
        }];
    }
    
    [recognizer setTranslation:CGPointZero inView:recognizer.view];
    
}



//侧拉没打开之前的边缘手势会调用
-(void)pan:(UIPanGestureRecognizer *)recognizer{
    //经过多次测试，如果需要改变收回或弹出距离，那只能用2个手势方法去做了。

    CGPoint trans = [recognizer translationInView:recognizer.view];
    CGFloat changeX = self.view.transform.tx + trans.x;
    if (changeX < 267 ) {
        self.view.transform = CGAffineTransformTranslate(self.view.transform, trans.x , 0);
        self.shadowView.alpha =  0.8 * changeX *0.001 +0.011;
    }
    
    if (recognizer.state == UIGestureRecognizerStateEnded && changeX > 267 * 0.2) {
        [UIView animateWithDuration:0.2 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(267, 0);
        }];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:0.2 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(-120, 0);
        }];
    }
    [recognizer setTranslation:CGPointZero inView:recognizer.view];
    
}

#pragma mark -
#pragma mark -tableview delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    YDYLog(@"%@",cell.textLabel.text);
}

#pragma mark -
#pragma mark -tableviewDatasource

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @" ";
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.settings.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dict = self.settings[section];
    NSArray *items = dict[@"item"];
    return items.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"set_cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    
    NSArray *setting =[self.settings[indexPath.section] valueForKey:@"item"];
    NSString *imgName = [setting[indexPath.row] valueForKey:@"icon"];
    NSString *name = [setting[indexPath.row] valueForKey:@"title"];
    
    cell.imageView.image = [UIImage imageNamed:imgName];
    cell.imageView.alpha = 0.5;
    cell.textLabel.text = [NSString stringWithFormat:@"    %@",name];
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.textLabel.textColor = [UIColor blackColor];
//    cell.backgroundColor = [UIColor clearColor];
    
   
    
    if (indexPath.row == 0 && indexPath.section == 0) {
        cell.textLabel.textColor = YDYThemeColor;
    }
    return cell;
}


#pragma mark -
#pragma mark -lazy loading
-(NSArray *)settings{
    if (_settings.count == 0) {
        
        _settings = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Setting.plist" ofType:nil]];
    }
    return _settings;
}
-(void)setPanGesture:(UIPanGestureRecognizer *)panGesture{
    _panGesture = panGesture;
    [self pan:panGesture];
    

}
@end
